package com.comdo.newup_loan.module.repayment_action;

import android.os.AsyncTask;

import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.http.RetrofitHelper;
import com.comdo.newup_loan.model.Result;
import com.comdo.newup_loan.model.ToXuDaiApply;
import com.comdo.newup_loan.model.UpLoadBean;
import com.comdo.newup_loan.util.BitmapUtil;
import com.comdo.newup_loan.util.StringUtils;
import com.google.gson.Gson;

import java.io.File;
import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by liyuan on 2017/11/7.
 */

class XudaiApplyPersenter extends XudaiApplyContact.Presenter {
    @Override
    public void getData(int repayPlanId, int userId, String rate) {
        Gson gson = new Gson();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        map.put("repayPlanId", repayPlanId);
        map.put("rate", rate);
        map.put("token", Config.loginInfo.getToken());
        map.put("sign", StringUtils.SortStringAndAddKey(map));
        String strEntity = gson.toJson(map);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        mView.showLoadingDialog("");
        mCompositeDisposable.add(RetrofitHelper.createMyApi().toGoOnLoanApply(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<ToXuDaiApply>>() {
                    @Override
                    public void accept(Result<ToXuDaiApply> Result) throws Exception {
                        mView.dismissLoadingDialog();
                        if (Result.getCode() == 1) {
                            mView.getData(Result.getResult());
                        } else {
                            mView.getFail(Result.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.dismissLoadingDialog();
                        mView.getFail("请求失败");
                    }
                }));
    }

    @Override
    public void getTip() {
        mCompositeDisposable.add(RetrofitHelper.createMyApi().loanApplyAttention()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<String>>() {
                    @Override
                    public void accept(Result<String> Result) throws Exception {
                        if (Result.getCode() == 1) {
                            mView.getTip(Result.getResult());
                        } else {
                            mView.getFail(Result.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.getFail("请求失败");
                    }
                }));
    }

    @Override
    public void upLoad(final int id, final String filePath,final int repayPlanId) {
        new AsyncTask<Integer, Integer, File>() {
            @Override
            protected File doInBackground(Integer... params) {
                //压缩图片
                File file = new File(BitmapUtil.compressImage(filePath));
                return file;
            }

            @Override
            protected void onPostExecute(File file) {
                super.onPostExecute(file);
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("userId", id);
                map.put("imgId", 0);
                map.put("repayPlanId", repayPlanId);
                map.put("token", Config.loginInfo.getToken());

                RequestBody requestFile =
                        RequestBody.create(MediaType.parse("multipart/form-data"), file);
                MultipartBody.Part fileBody =
                        MultipartBody.Part.createFormData("img", file.getName(), requestFile);
                MultipartBody.Part userId =
                        MultipartBody.Part.createFormData("userId", String.valueOf(id));
                MultipartBody.Part repayPlanIdPart =
                        MultipartBody.Part.createFormData("repayPlanId", String.valueOf(repayPlanId));
                MultipartBody.Part imgIdPart =
                        MultipartBody.Part.createFormData("imgId", String.valueOf(0));
                MultipartBody.Part token =
                        MultipartBody.Part.createFormData("token", Config.loginInfo.getToken());
                MultipartBody.Part sign =
                        MultipartBody.Part.createFormData("sign", StringUtils.SortStringAndAddKey(map));
                mView.showLoadingDialog("");
                mCompositeDisposable.add(RetrofitHelper.createMyApi().imageUpload(fileBody, userId,repayPlanIdPart, imgIdPart,token, sign)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<Result<UpLoadBean>>() {
                            @Override
                            public void accept(Result<UpLoadBean> Result) throws Exception {
                                mView.dismissLoadingDialog();
                                if (Result.getCode() == 1) {
                                    mView.upLoad(Result.getResult());
                                } else {
                                    mView.getFail(Result.getMessage());
                                }
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                mView.dismissLoadingDialog();
                                mView.getFail("请求失败");
                            }
                        }));
            }
        }.execute();

    }

    @Override
    public void goOnLoanApply(int repayPlanId, int userId, String rate, ToXuDaiApply toXuDaiApply, String urls) {
        Gson gson = new Gson();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        map.put("repayPlanId", repayPlanId);
        map.put("rate", rate);
        map.put("token", Config.loginInfo.getToken());

        map.put("principle", toXuDaiApply.getPrinciple());
        map.put("totalPay", toXuDaiApply.getTotalPay());

        map.put("carStopCost", toXuDaiApply.getCarStopCost());
        map.put("gpsUseCost", toXuDaiApply.getGpsUseCost());
        map.put("manageCost", toXuDaiApply.getManageCost());
        map.put("riskCost", toXuDaiApply.getRiskCost());
        map.put("sqdjCost", toXuDaiApply.getSqdjCost());

        map.put("carStopCostPercent", toXuDaiApply.getCarStopCostPercent());
        map.put("gpsUseCostPercent", toXuDaiApply.getGpsUseCostPercent());
        map.put("riskCostPercent", toXuDaiApply.getRiskCostPercent());
        map.put("interestCostPercent", toXuDaiApply.getInterestCostPercent());
        map.put("manageCostPercent", toXuDaiApply.getManageCostPercent());

        if (!StringUtils.isNull(urls))
            map.put("picUrls", urls);
        map.put("sign", StringUtils.SortStringAndAddKey(map));
        String strEntity = gson.toJson(map);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        mView.showLoadingDialog("");
        mCompositeDisposable.add(RetrofitHelper.createMyApi().goOnLoanApply(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<String>>() {
                    @Override
                    public void accept(Result<String> Result) throws Exception {
                        mView.dismissLoadingDialog();
                        if (Result.getCode() == 1) {
                            mView.goOnLoanApply(Result.getResult());
                        } else {
                            mView.getFail(Result.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.dismissLoadingDialog();
                        mView.getFail("请求失败");
                    }
                }));
    }

    @Override
    public void delImg(int userId, int imgId) {
        Gson gson = new Gson();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        map.put("imgId", imgId);
        map.put("token", Config.loginInfo.getToken());
        map.put("sign", StringUtils.SortStringAndAddKey(map));
        String strEntity = gson.toJson(map);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        mView.showLoadingDialog("");
        mCompositeDisposable.add(RetrofitHelper.createMyApi().delPic(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<String>>() {
                    @Override
                    public void accept(Result<String> Result) throws Exception {
                        mView.dismissLoadingDialog();
                        if (Result.getCode() == 1) {
                            mView.delImg(Result.getMessage());
                        } else {
                            mView.delImgFail(Result.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.dismissLoadingDialog();
                        mView.getFail("请求失败");
                    }
                }));
    }


}
